package com.shanghaionstar.weight;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

@SuppressLint("DrawAllocation")
public class DialPlateView extends View {
    private Paint mFuelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Shader mShader;
    private Paint mPointerPaint = new Paint();
    private Paint maskPaint = new Paint();
    private Paint bgPaint = new Paint();
    private PathEffect effect;
    private Matrix matrix = new Matrix();

    private int[] dialColors = new int[]{
            0xFFb91497,  //135
            0xFFf7464b,  //180
            0xFFe3e533,  //225
            0xFF6de569,  //270
            0xFF10eac2,  //315
            0xFF09d4f3,  //0
            0xFF3a89f3,  //45
            0xFF191970,  //90

    };

    private int grayDialColor = 0xFFc6cddd;

    private static final float startAngel = 135;

    private static final float endAngel   = 405;

    private float betweenAngel = endAngel - startAngel;

    private float sweepAngle = 0;


    private float cx,cy;

    public DialPlateView(Context context) {
        super(context);
        init();
    }

    public DialPlateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setFocusable(true);
        setFocusableInTouchMode(true);

        mFuelPaint.setStyle(Paint.Style.STROKE);
        effect = new DashPathEffect(new float[]{15, 10, 15, 10}, 0);
        mFuelPaint.setPathEffect(effect);
        mFuelPaint.setStrokeWidth(50);

        maskPaint.setColor(0xFFf1f1f5);
        maskPaint.setStrokeWidth(50);

        mPointerPaint.setColor(0xFF18acfe);
        mPointerPaint.setStrokeWidth(2);
        mPointerPaint.setAntiAlias(true);

        bgPaint.setColor(0xFFf1f1f5);
        bgPaint.setStrokeWidth(50);
        bgPaint.setStyle(Paint.Style.STROKE);
        bgPaint.setAntiAlias(true);
    }



    @Override
    protected void onDraw(Canvas canvas) {
        if (mShader == null) {
            cx = getMeasuredWidth() / 2;
            cy = getMeasuredHeight() / 2;
            mShader = new SweepGradient(cx, cy, dialColors, null);
        }
        canvas.drawColor(Color.WHITE);
        drawDial(canvas, cx, cy, cy, startAngel, endAngel, mFuelPaint);
        drawPointer(canvas, cx,cy,startAngel);
// for test

//        if (sweepAngle < 270) {
//            sweepAngle += 10;
//            invalidate();
//        }
    }

    /**
     * 根据值显示
     */
    public void showByValue(float sweepValue) {
        if (sweepValue > 270 || sweepValue < 0)
            throw new IllegalArgumentException("输入的角度不在表盘区间内");
        ValueAnimator animator = ValueAnimator.ofFloat(0,270, sweepValue);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                sweepAngle = (Float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.setDuration(1000);
        animator.setStartDelay(100);
        animator.start();
    }

    /**
     * 根据百分比显示
     * @param sweepPercent
     */
    public void showByPercent(float sweepPercent) {
        if (sweepPercent > 1 || sweepPercent < 0)
            throw new IllegalArgumentException("输入的角度不在表盘区间内");
        ValueAnimator animator = ValueAnimator.ofFloat(0, 1, sweepPercent);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                sweepAngle = (Float) animation.getAnimatedValue() * betweenAngel;
                invalidate();
            }
        });
        animator.setDuration(1500);
        animator.setStartDelay(100);
        animator.start();
    }

    /**
     * 绘制表盘
     */
    public void drawDial(Canvas canvas, float cx, float cy, float r,
                         float startAngel, float endAngel, Paint paint) {
        RectF rectF = new RectF(paint.getStrokeWidth() + 20, paint.getStrokeWidth() + 20, cx + r - paint.getStrokeWidth() - 20, cy + r - paint.getStrokeWidth() - 20);
        canvas.drawArc(rectF, startAngel, endAngel - startAngel, false, bgPaint);
        paint.setShader(null);
        paint.setColor(grayDialColor);
        rectF.set(paint.getStrokeWidth(), paint.getStrokeWidth(), cx + r - paint.getStrokeWidth(), cy + r - paint.getStrokeWidth());
        canvas.drawArc(rectF, startAngel, endAngel - startAngel, false, paint);
        matrix.setRotate(startAngel, cx, cy);
        mShader.setLocalMatrix(matrix);
        paint.setShader(mShader);
        canvas.drawArc(rectF, startAngel, sweepAngle, false, paint);
    }

    /**
     * 绘制指针
     */
    private void drawPointer(Canvas canvas,float cx,float cy, float startAngel) {
        mPointerPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawCircle(cx, cy, 20, mPointerPaint);
        mPointerPaint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(cx, cy, 30, mPointerPaint);
        Path path = new Path();
        path.moveTo(cx, cy);
        float r = cx - bgPaint.getStrokeWidth() - 50;
//        path.moveTo((float) (cx - 20* Math.cos(startangel * Math.PI / 180)),
//                (float) (cy - 20* Math.sin(startangel * Math.PI / 180)));
        path.lineTo((float) (cx + r * Math.cos((startAngel+sweepAngle) * Math.PI / 180))
                , (float) (cy + r * Math.sin((startAngel+sweepAngle) * Math.PI / 180)));

//        path.lineTo((float) (cx + r * Math.cos(startangel * Math.PI / 180))
//                , (float) (cy + r * Math.sin(startangel * Math.PI / 180)));
        canvas.drawPath(path, mPointerPaint);
    }
}